﻿using System;
using System.Html;
using Quicklight.Web.Html;
using jQueryApi;

namespace Quicklight.Web.Razor {
   
   /// <summary>
   /// Does the binding on behalf of a declarative binding of a CalculatedProperty to an attribute on an element
   /// For example <input type="text" @Binding("value", ViewModel.Result) />
   /// </summary>
   /// <typeparam name="T">Type of the Value property of the CalculatedProperty</typeparam>
   public class CalculatedPropertyAttributeBinder<T> : AttributeBinder {
      private CalculatedProperty<T> property;

      /// <summary>
      /// Constructs a CalculatedPropertyAttributeBinder for binding the CalculatedProperty to the attribute 
      /// </summary>
      /// <param name="attributeName">Name of attribute to bind to</param>
      /// <param name="property">CalculatedProperty to bind to attribute</param>
      public CalculatedPropertyAttributeBinder(string attributeName, CalculatedProperty<T> property)
         : base(attributeName) {

         this.property = property;
      }

      /// <summary>
      /// Bind method does the actual binding of the CalculatedProperty to the attribute of the specified element
      /// </summary>
      /// <param name="element">Element with which the binding must occur</param>
      protected override void Bind(Element element) {
         element.Bind<T>(attributeName, property);

         jQueryObject jQueryObject = jQuery.FromElement(element);
         jQueryObject.Attribute(attributeName, property.Value.ToString());
      }
   }
}
